#!/bin/bash
# 同时启动后端和前端服务

echo "=== 启动生物信息数据库管理系统 ==="
echo ""

# 获取脚本目录
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

# 检查 MySQL 服务
if ! systemctl is-active --quiet mysql && ! systemctl is-active --quiet mariadb; then
    echo "警告: MySQL/MariaDB 服务未运行"
    echo "请先启动 MySQL 服务: sudo systemctl start mysql"
    echo ""
fi

# 启动后端（后台运行）
echo "启动后端服务..."
bash "$SCRIPT_DIR/start_backend.sh" &
BACKEND_PID=$!

# 等待后端启动
sleep 3

# 启动前端（后台运行）
echo "启动前端服务..."
bash "$SCRIPT_DIR/start_frontend.sh" &
FRONTEND_PID=$!

echo ""
echo "==================================="
echo "✓ 所有服务已启动"
echo "==================================="
echo ""
echo "服务地址："
echo "  - 前端 Web 界面: http://localhost:3000"
echo "  - 后端 API: http://localhost:8000"
echo "  - API 文档: http://localhost:8000/docs"
echo ""
echo "启动 TUI 界面："
echo "  bash $SCRIPT_DIR/start_tui.sh"
echo ""
echo "停止所有服务："
echo "  kill $BACKEND_PID $FRONTEND_PID"
echo "  或按 Ctrl+C"
echo ""

# 等待用户中断
trap "kill $BACKEND_PID $FRONTEND_PID 2>/dev/null; echo '服务已停止'; exit 0" INT TERM

wait